package com.wlr.study.pointsword;

/**
 * 从尾到头打印链表
 * 输入一个链表的头节点，从尾到头反过来返回每个节点的值（用数组返回）。
 * // 输入：head = [1,3,2]
 * //输出：[2,3,1]
 *
 * @author lrwang
 * @version 1.0
 * @date 2022/3/30 20:23
 */
public class Offer06 {
    public static int[] reversePrint(ListNode head) {
        ListNode node = head;
        int count = 0;
        while (node != null) {
            ++count;
            node = node.next;
        }
        int[] nums = new int[count];
        node = head;
        for (int i = count - 1; i >= 0; --i) {
            nums[i] = node.val;
            node = node.next;
        }
        return nums;
    }

    public static void main(String[] args) {
        ListNode head=new ListNode(1);
        ListNode mid=new ListNode(3);
        ListNode last=new ListNode(2);
        head.next=mid;
        mid.next=last;
        Offer06.reversePrint(head);
    }
}
